package com.haojin.java;

import org.junit.Test;

import java.io.*;

/**
 * @author .29.
 * @create 2022-04-15 7:08
 */
/*
测试FileInputStream和FileOutputStream的使用

结论：
1.对于文本文件（.txt,.c,.cpp）,使用字符流处理
2.非文本文件（.jpg,.mp3,.mp4,.avi,.doc,.ppt,....）使用字节流处理
 */
public class FileInputOutputStreamTest {
    //使用字节流FileInputStream处理文本文件可能出现乱码
    @Test
    public void test1()   {

        FileInputStream fi = null;
        try {
            //File类的实例化
            File file = new File("hi hi hi.txt");
            //创建流的对象
            fi = new FileInputStream(file);
            //读取操作
            byte[] buffer = new byte[5];
            int len;//记录每次读取的个数
            while((len = fi.read(buffer)) != -1){
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fi != null){
                try {

                    //关闭流资源
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    //实现对图片的复制
    @Test
    public void test2()   {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File("p.JPG");
            File preFile = new File("p1.JPG");
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(preFile);
            byte[] buffer = new byte[8];
            int len;
            while((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //指定路径下的文件的复制
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File(srcPath);
            File preFile = new File(destPath);
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(preFile);
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void testCopyFile(){
        long startTime = System.currentTimeMillis();

        String scrPath = "C:\\Users\\10575\\Desktop\\q1.mp4";
        String destPath = "C:\\Users\\10575\\Desktop\\q2.mp4";
        copyFile(scrPath,destPath);

        long endTime = System.currentTimeMillis();
        System.out.println("耗时："+(endTime-startTime));
    }
}
